﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditLandTour : AdminPageBase
    {
        private LandTourInfo _landtourInfo = new LandTourInfo();
        private readonly LandTourController _landTourController = new LandTourController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["LandTourId"]))
                _landtourInfo = _landTourController.GetLandTour(int.Parse(Request.QueryString["LandTourId"]));

            if (Page.IsPostBack) return;

           LoadPartner();
           if (_landtourInfo.LandTourId == -1) return;

         RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListLandTour.aspx?xml=LandTour");
        }

        private void SaveData()
        {
            _landtourInfo.LandTourName = txtLandTourName.Text;
            _landtourInfo.PartnerId = int.Parse(ddlPartner.SelectedValue);
            if (_landtourInfo.LandTourId == -1)
            {
               _landTourController.InsertLandTour(_landtourInfo);
            }
            else
            {
               _landTourController.UpdateLandTour(_landtourInfo);
            }

            Response.Redirect("/Pages/ListLandTour.aspx?xml=LandTour");
        }

        private void RenderForm()
        {
           ddlPartner.SelectedValue = _landtourInfo.PartnerId.ToString();
       }
       private void LoadPartner()
        {
            ddlPartner.DataSource = new PartnerController().ListPartner()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.PartnerName).ToList();
            ddlPartner.DataTextField = "PartnerName";
            ddlPartner.DataValueField = "PartnerId";
            ddlPartner.DataBind();

            ddlPartner.Items.Insert(0, new ListItem("--- Chọn nhà cung cấp ---", "-1"));
        }
    }
}